package com.example.tableapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.StrictMode;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.tableapplication.application.MyApplication;
import com.example.tableapplication.dao.SOAPUtil;
import com.google.gson.JsonObject;


public class MainActivity_3 extends AppCompatActivity implements View.OnClickListener{

    MyApplication myApplication;  //获取全局变量
    private EditText et_dbadress;   //数据库地址
    private EditText et_dbport;     //数据库端口
    private EditText et_dbname;     //数据库名称
    private EditText et_dbusername; //数据库用户名
    private EditText et_dbpassword; //数据库密码
    private Button bt_dbsave;       //保存按钮
    private Button bt_dbtest;       //测试按钮
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_3);
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
        myApplication= (MyApplication) getApplication();
        initView(); //初始化控件

    }



    /*
    初始化控件
     */
    public void initView(){
        //绑定控件
        et_dbadress=findViewById(R.id.et_dbadress);
        et_dbport=findViewById(R.id.et_dbport);
        et_dbname=findViewById(R.id.et_dbname);
        et_dbusername=findViewById(R.id.et_dbusername);
        et_dbpassword=findViewById(R.id.et_dbpassword);
        bt_dbsave=findViewById(R.id.bt_dbsave);
        bt_dbtest=findViewById(R.id.bt_dbtest);
        et_dbpassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
        //为控件设置初始值（从全局变量MyApplication中获取）
        if(myApplication.getDbsource_state()){
            et_dbadress.setText(myApplication.getEt_dbadress());
            et_dbport.setText(myApplication.getEt_dbport());
            et_dbname.setText(myApplication.getEt_dbname());
            et_dbusername.setText(myApplication.getEt_dbusername());
            et_dbpassword.setText(myApplication.getEt_dbpassword());
        }

        //确定按钮注册监听
        bt_dbsave.setOnClickListener(this);
        bt_dbtest.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_dbsave:
                dbsource_save();
                break;
            case R.id.bt_dbtest:
                String test=dbsource_test().toString().trim();
                MainActivity_1.showDialog(MainActivity_3.this,test);
                break;
        }
    }

    public void dbsource_save(){
        //将各控件保存到全局变量中以及本地
        myApplication.setDbsource_state(true);
        myApplication.setEt_dbadress(et_dbadress.getText().toString());
        myApplication.setEt_dbport(et_dbport.getText().toString());
        myApplication.setEt_dbname(et_dbname.getText().toString());
        myApplication.setEt_dbusername(et_dbusername.getText().toString());
        myApplication.setEt_dbpassword(et_dbpassword.getText().toString());
        myApplication.saveManger();
    }
    public String dbsource_test(){
        String dbip=myApplication.getEt_dbadress();
        String dbport=myApplication.getEt_dbport();
        String dbname=myApplication.getEt_dbname();
        String dbusername=myApplication.getEt_dbusername();
        String dbpassword=myApplication.getEt_dbpassword();
        String wsdUrl="http://"+dbip+":9090/ConnectToU8/services/TableInfoService?wsdl";
        JsonObject json =new JsonObject();
        json.addProperty("dbip",dbip);
        json.addProperty("dbport",dbport);
        json.addProperty("dbname",dbname);
        json.addProperty("dbusername",dbusername);
        json.addProperty("dbpassword",dbpassword);
        Object result= SOAPUtil.doTransport(wsdUrl, "getConnectionTest",json.toString());
        return result.toString();
    }

}